package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrdersService ordersService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders,HttpSession session)
    {
        log.info("orders:"+orders);
        ordersService.sumbit(orders,session);
        return R.success("下单成功");
    }
    @GetMapping("/page")
    public R<Page>page(int page, int pageSize,@RequestParam(value = "beginTime",required = false)String beginTime,
                       @RequestParam(value = "endTime",required = false) String endTime,
                       @RequestParam(value = "number",required = false)Long id)
    {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        lambdaQueryWrapper.eq(id!=null,Orders::getId,id);
        if(beginTime==null&&endTime==null)
        {
            ordersService.page(pageInfo,lambdaQueryWrapper);
            return R.success(pageInfo);
        }
        try {
            Date bTime = simpleDateFormat.parse(beginTime);
            Date eTime = simpleDateFormat.parse(endTime);
            lambdaQueryWrapper.ge(Orders::getOrderTime,bTime);
            lambdaQueryWrapper.le(Orders::getOrderTime,eTime);
            ordersService.page(pageInfo,lambdaQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.success(pageInfo);
    }
    @PutMapping()
    public R<String>disPatch(@RequestBody Orders orders)
    {
        int status=orders.getStatus();
        Long id = orders.getId();
        LambdaUpdateWrapper<Orders>lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Orders::getId,id);
        lambdaUpdateWrapper.set(Orders::getStatus,status);
        ordersService.update(lambdaUpdateWrapper);
        return R.success("派送成功");
    }
    @GetMapping("/userPage")
    public R<Page>page(int page, int pageSize, HttpSession session)
    {
        Long userId= (Long) session.getAttribute("user");
        Page<Orders>ordersPage=new Page<>(page,pageSize);
        Page<OrderDto>pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Orders::getUserId,userId);
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,lambdaQueryWrapper);
        BeanUtils.copyProperties(ordersPage,pageInfo,"records");
        List<Orders> records = ordersPage.getRecords();
        List<OrderDto>dtoRecords=records.stream().map((item)->
        {
            OrderDto orderDto=new OrderDto();
            BeanUtils.copyProperties(item,orderDto);
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail>lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> list1 = orderDetailService.list(lambdaQueryWrapper1);
            orderDto.setOrderDetail(list1);
            return orderDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(dtoRecords);
        return R.success(pageInfo);
    }
    @PostMapping("/again")
    @Transactional
    public R<String>again(@RequestBody Orders orders){
        Long id = orders.getId();
        LambdaQueryWrapper<Orders>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Orders::getId,id);
        Orders orders1 = ordersService.getOne(lambdaQueryWrapper);
        ordersService.again(orders1);
        return R.success("获取一单成功");
    }
}
